import { ElNotification, ElMessage, ElMessageBox } from 'element-plus';
import { ref } from 'vue';
/**
 * @description 一键同步数据
 * @param {Function} api 同步表格的api方法(必传)
 * @param {Object} params 同步的参数(默认为空对象)
 * @return void
 * */
const isAynch = ref(true);
export const useAynchronization = async (api: (param: any) => Promise<any>, params: any = {}, message: string = '') => {
	if (!isAynch.value) {
		ElNotification({
			title: '温馨提示',
			message: '正在同步中，请稍等',
			type: 'info',
			duration: 3000
		});
		return;
	}

	const aynchronizationFn = async () => {
		try {
			isAynch.value = false;
			ElMessage({
				message: '正在同步中，请稍等',
				duration: 4000,
				type: 'success'
			});
			const res = await api(params);
			console.log(res);
			const messageConfirm = `本知识中台${message}信息同步成功，已成功更新本地知识库`;
			ElMessageBox.confirm(messageConfirm, '提示信息', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'success', // 成功同步success / 失败warning
				center: true
			})
				.then(() => {})
				.catch(() => {});

			isAynch.value = true;
		} catch (error) {
			console.log(error);
			isAynch.value = true;
		}
	};

	aynchronizationFn();
};
